
package org.fintool.parser;

import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.fintool.common.Constants;
import org.fintool.http.HTTPUtil;

public class MarkupParser implements IParser {
	
	
	
	
	
	public  String getElement(final String strURL,final String elementDefinition )throws Exception{
		
		
		String retElemetvalue=null;
		HTTPUtil httpUtil=null;
		String strFullHTMLPage=null;
		
		ArrayList definitionList=null;
		Iterator defIterator=null;
		
		String definition=null;
		String parseResult=null;
		
		try{
			
			httpUtil=new HTTPUtil();
			strFullHTMLPage=httpUtil.getHTTPContentAsString(new URL(strURL));
			
			if(strFullHTMLPage!=null){
				definitionList=getDefinitionList(strURL);
				if(definitionList!=null){
					defIterator=definitionList.iterator();
					parseResult=new String(strFullHTMLPage);
					while (defIterator.hasNext()) {
						definition=defIterator.next().toString();
						parseResult=parseForDefintion(parseResult,definition);
					}
					
					retElemetvalue=new String(parseResult);
					
					
				}
				
				
				
				
				
			}else{
				retElemetvalue=Constants.NO_PAGE;
			}
			
		}
		catch (Exception e) {
			// TODO: handle exception
			throw e;
		
		}
		
		
		
		
		
		return retElemetvalue;
		
		
	}
	
	//Parses markup and returns between Tags <>  and </>
	/**
	 * @param strSourceString
	 * @param strDefinition
	 * @return
	 */
	private  final String parseForDefintion(final String strSourceString,final String strDefinition){
		String strResult=null;
		
		String strMarkup=findMatchedString(strSourceString,strDefinition);
		
		String strArrString[]=null;
		if(strMarkup!=null){
			
		
			if(strMarkup.indexOf(">")>0 && strMarkup.indexOf("</") >0 ){
			//System.out.println(strMarkup);
				strArrString=strMarkup.split(",");
				if(strArrString!=null&&strArrString.length>0){
			for(int i=1;i<strArrString.length;i++){	
			strResult+=","+strArrString[i].substring(strArrString[i].indexOf('>')+1, strArrString[i].indexOf("</"));
			 }
		    }else{
		    	strResult=strMarkup.substring(strMarkup.indexOf('>')+1, strMarkup.indexOf("</"));
		    	
		    }
			
		}
		}
		
		
		
		
		return strResult;
		
	}
	
	private final String findMatchedString(final String strSource,final String strDefinition){
		
		String strReturn ="";
		
		Pattern p = Pattern.compile(strDefinition);
        Matcher m = p.matcher(strSource); // get a matcher object
      
        
       // System.out.println(m.find());
        
      //  System.out.println(m.groupCount());
       while( m.find()){
        
        strReturn+=","+m.group();
       }
        
        
        
        return strReturn;
	}

	
	private  final ArrayList getDefinitionList(final String strDefinition) throws Exception{
		ArrayList defList=null;
		String[] defs=null;
		try{
			defs=strDefinition.split(Constants.PIPE_EXPRESSION);
			if(defs!=null){
				
				defList=new ArrayList();
				
				for(int i=0;i<defs.length;i++){
					defList.add(defs[i]);
				}
				
			}
		}catch (Exception e) {
			throw e;
			// TODO: handle exception
		}
		
		return defList;
		
	}
	
	public void writeRawFileData(String stock,String exchange,
			ArrayList dateRange) throws Exception {
		// TODO Auto-generated method stub

	ArrayList alRawFileData=null;

	//for a Simple day file we will get date as null so parse for currentDay and year
	if(dateRange==null){
	  
	}
	//Else use URL maker with timestamp data
	else{

	}
	}
	
public ArrayList getRawFileData(String stock,String exchange,
		ArrayList dateRange) throws Exception {
	// TODO Auto-generated method stub
	return null;
}
	
	
	//Main method
	
	
	public static void main(String[] args) throws Exception {
		MarkupParser pr=new MarkupParser();
		
		//System.out.println(pr.findMatchedString("<span class=\"ch bld\"><span class=\"chr\" id=\"ref_689660_c\">-0.89</span>","<span class=\"chr\".*</span>"));
		
		System.out.println(pr.parseForDefintion(new HTTPUtil().getHTTPContentAsString(new URL("http://www.google.com/finance?q=ASX:GLD")),Constants.G_CHANGE_REGEX_POINTS_MARKUP));
		//System.out.println(pr.findMatchedString("chr io","chr"));
		
	}
	
	
	
	
}
